package jframe1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo1_1 extends JFrame implements ActionListener{

	JPanel mp = null;
	JButton jb1 = null;
	JButton jb2 = null;
	public Demo1_1(){

		mp = new JPanel();
		mp.setBackground(Color.BLUE);

		jb1 = new JButton("黑色");
		jb2 = new JButton("红色");

		Cat c = new Cat();

		this.add(jb1,BorderLayout.NORTH);
		this.add(mp);
		this.add(jb2,BorderLayout.SOUTH);
		//注册监听    指定action命令 		一个事件源可被多个对象监听
		jb1.addActionListener(this);
		jb1.setActionCommand("aa");
		jb1.addActionListener(c);
		jb2.addActionListener(this);
		jb2.setActionCommand("bb");
		this.setSize(300,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		Demo1_1 d1 = new Demo1_1();
	}

	/**
	 * 对事件处理的方法
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("aa")){
			System.out.println("点击黑色按钮");
			mp.setBackground(Color.BLACK);
		}
		else if(e.getActionCommand().equals("bb")){
			System.out.println("点击红色按钮");
			mp.setBackground(Color.RED);
		}else{
			System.out.println("不知道！！！");
		}
	}


}



